package com.volley.help;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;

public class ImageLoaderConfiguration {
	public static final int MAX_CACHE_SIZE = 10 * 1024 * 1024;
	public Context context;
	public Resources resources;
	public int maxWidth;
	public int maxHeight;
	public int maxMemorySize;
	public int maxCacheSize;
	public LruMemoryCache mMemoryCache;

	private ImageLoaderConfiguration(Builder builder) {
		resources = builder.mContext.getResources();
		context = builder.mContext;
		maxWidth = builder.maxWidth;
		maxHeight = builder.maxHeight;
		maxMemorySize = builder.maxMemorySize;
		maxCacheSize = builder.maxCacheSize;
		mMemoryCache = builder.mMemoryCache;
	}

	public ImageSize getImageSize() {
		DisplayMetrics metrics = resources.getDisplayMetrics();
		if (maxWidth <= 0) {
			maxWidth = metrics.widthPixels;
		}
		if (maxHeight <= 0) {
			maxHeight = metrics.heightPixels;
		}
		return new ImageSize(maxWidth, maxHeight);
	}

	public static class Builder {

		public Context mContext;
		public int maxWidth;
		public int maxHeight;
		public int maxMemorySize;
		public int maxCacheSize;
		public LruMemoryCache mMemoryCache;

		public Builder(Context context) {
			mContext = context;
		}

		public Builder setBitmapOptions(int width, int height) {
			maxWidth = width;
			maxHeight = height;
			return this;
		}

		public Builder setMaxMemorySize(int memorySize) {
			maxMemorySize = memorySize;
			return this;
		}

		public Builder setMaxCacheSize(int cacheSize) {
			maxCacheSize = cacheSize;
			return this;
		}

		public Builder memoryCache(LruMemoryCache lruCache) {
			mMemoryCache = lruCache;
			return this;
		}

		public ImageLoaderConfiguration build() {
			return new ImageLoaderConfiguration(this);
		}

		public void initConfiguration() {
			if (mMemoryCache == null) {
				if (maxMemorySize == 0) {
					maxMemorySize = (int) (Runtime.getRuntime().maxMemory() / 8);
				}
				mMemoryCache = new LruMemoryCache(maxMemorySize);
			}
			if (maxCacheSize == 0) {
				maxCacheSize = MAX_CACHE_SIZE;
			}
		}
	}

	public static class ImageSize {
		public int width;
		public int height;

		public ImageSize(int width, int height) {
			this.width = width;
			this.height = height;
		}
	}

}
